/**
 * Copyright 2010, Lars J. Nilsson <http://www.larsan.net> 
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.larsan.dconf.jbc;

import java.util.prefs.Preferences;
import java.util.prefs.PreferencesFactory;

import net.larsan.dconf.DNode;
import net.larsan.dconf.DNodeStore;
import net.larsan.dconf.DPath;
import net.larsan.dconf.prefs.DNodePreferences;
import net.larsan.dconf.util.DPaths;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.name.Named;

public class JBCPreferencesFactory implements PreferencesFactory {
	
	public static final String USER_DOMAIN_OVERRIDE_PROP = "net.larsan.dconf.jbc.userDomainOverride";

	@Inject
	private DNodeStore store;
	
	@Inject
	@Named("userDomain")
	private String userDomain;
	
	public JBCPreferencesFactory() {
		// TODO Fix JBC configuration here.. .
		Guice.createInjector(new ModuleAggregate()).injectMembers(this);
	}
	
	@Override
	public Preferences systemRoot() {
		return new DNodePreferences(store.getRootDomainNode());
	}
	
	@Override
	public Preferences userRoot() {
		String domain = System.getProperty(USER_DOMAIN_OVERRIDE_PROP);
		if(domain == null) {
			domain = userDomain;
		}
		DPath path = DPaths.safeParse(domain + ":/");
		DNode node = store.get(path, true);
		return new DNodePreferences(node);
	}
}
